/**
 * 
 */
package at.fhooe.mcm12.java.clients;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Scanner;

import at.fhooe.mcm12.java.interfaces.RMISensorInterface;
import at.fhooe.mcm12.java.sensors.AbstractSensor;
import at.fhooe.mcm12.java.sensors.data.SensorValue;
import at.fhooe.mcm12.java.utils.TimeUtils;

/**
 * @author Elisabeth Mair
 *
 */
public class ConsoleClient extends AbstractClient {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ConsoleClient(){
		super();
		ArrayList<RMISensorInterface> sensorList = null;
		try {
			sensorList = m_clientManager.getSensors();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		if(sensorList!=null && sensorList.size()>0){
			
			//register for all sensors
			for (RMISensorInterface abstractSensor : sensorList) {
				
				try {
					m_clientManager.registerFor(this, abstractSensor);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
			
		}else{
			System.out.println("sensorList is null or has no entries");
		}
		// Call to enable() is necessary to receive sensor updates.
		try {
			m_clientManager.enable(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConsoleClient consoleClient = new ConsoleClient();

	}

	@Override
	public void onSensorValueReceived(String sensor, SensorValue value) {
		final String s = "[" + sensor + "]: "
				+ TimeUtils.getCurrentTimeStamp(value.getTimestamp()) + ": "
				+ value.toString();
		
		System.out.println("Console-Client " + s);
	}

	@Override
	public String getName() {
		return "Console Client";
	}
}
